Bee.data.DWRPageProxy = function(dwrCall, config, ownerCt){
    config = config ? config : {};
    this.dwrCall = dwrCall;
    this.filter = (config.filter ? config.filter : true);
    this.like = (config.like ? config.like : true);
    this.sort = (config.sort ? config.sort : true);
    this.ownerCt = ownerCt;
    Bee.data.DWRPageProxy.superclass.constructor.call(this);
};

Ext.extend(Bee.data.DWRPageProxy, Ext.data.DataProxy, {
    load: function(params, reader, callback, scope, arg){
        if (this.fireEvent("beforeload", this, params) !== false) {
            var delegate = this.loadResponse.createDelegate(this, [reader, callback, scope, arg], 1);
            var callParams = [];
            if (arg.arg) {
                callParams = arg.arg.slice();
            }
            if (this.filter) {
                if (params.filter) {
                    callParams.push(params.filter);
                }
                else {
                    callParams.push({});
                }
            }
            if (this.like) {
                if (params.like) {
                    callParams.push(params.like);
                }
                else {
                    callParams.push({});
                }
            }
            if (this.sort) {
                var sort = {};
                if (params.sort && typeof params.sort == 'object') {
                    sort = params.sort;
                }
                else 
                    if (params.sort && params.dir) {
                        sort[params.sort] = params.dir;
                    }
                callParams.push(sort);
            }
            Ext.applyIf(params, {
                start: 0,
                limit: 20
            });
            callParams.push(Math.ceil(params.start / params.limit) + 1);
            callParams.push(params.limit);
            if (this.ownerCt) {
                var ownerCt = this.ownerCt;
                delegate = {
                    callback: delegate
                };
            }
            callParams.push(delegate);
            this.dwrCall.apply(this, callParams);
        }
        else {
            callback.call(scope || this, null, arg, false);
        }
    },
    loadResponse: function(response, reader, callback, scope, arg){
        var result;
        try {
            result = reader.read(response);
        } 
        catch (e) {
            this.fireEvent("loadexception", this, null, response, e);
            callback.call(scope, null, arg, false);
            return;
        }
        callback.call(scope, result, arg, true);
    }
});
